<template>
	<view class="container2">
		<form @submit="formSubmit">
			<view class="square1">
				<view class="subname">{{SubName}}</view>
			</view>
			<view class="square1" v-if="(OldList.length+ImgList.length)>0">
				<view class="line1">
					<view class="img border" v-for="(img,index) in OldList"><image :src="Tools.GetImg(img,500)" mode="aspectFill" @click="PrevImg(OldList,index)"/><view class="delete" @click="DeleteImg1(index)" @click.stop="" v-if="Entity.State==1"><image src="/static/error.png"/></view></view>
					<view class="img border" v-for="(img,index) in ImgList"><image :src="img" mode="aspectFill" @click="PrevImg(ImgList,index)"/><view class="delete" @click="DeleteImg2(index)" @click.stop="" v-if="Entity.State==1"><image src="/static/error.png"/></view></view>
				</view>
			</view>
			<template v-else>
				<view class="nodata"></view>
				<view class="loadmore">暂无图片</view>
			</template>				
			<template v-if="Entity.State==1">
				<view class="upimg" @click="ChooseImg()">点此选择所要上传的文书图片</view>
				<view class="btn"><button class="publishs" form-type="submit">确定</button></view>
			</template>
			<view class="cu-form-group border-b" v-if="Method.indexOf('_Send')>-1">
				<view class="title"><text class="red">*</text>送达时间</view>
				<view class="text">
					<uni-timepicker fields="day" placeholder="选择时间" :defaultValue="Info.SendDate" @change="SendDateChange"></uni-timepicker>
					<input name="SendDate" type="text" class="hidden" :value="Info.SendDate"/>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ID:0,
				Info:{},
				Entity:{},
				CaseID:0,
				RoleID:0,
				SubName:'',
				Method:'',
				OldList:[],
				ImgList:[]
			};
		},
		onLoad(options) {
			uni.setNavigationBarTitle({title:options.SubName});
			this.SubName=options.SubName;
			this.CaseID=options.CaseID;
			this.Method=options.Method;
			this.RoleID=uni.getStorageSync("RoleID");
			this.Find_CaseInfo();
		},
		methods:{
			SendDateChange(e){
				this.Info.SendDate = e.f1;
			},
			Find_CaseInfo(){
				var data = {};
				data.Act="Find_AnnexInfo";
				data.CaseID=this.CaseID;
				data.Method=this.Method;
				this.Util.Loading("加载中...");
				this.Util.Post(data, json=>{
					this.Util.HideLoading();
					uni.stopPullDownRefresh();
					uni.hideNavigationBarLoading();
					this.ID=json.Data.Info.ID;
					this.Info=json.Data.Info;
					this.Entity=json.Data.Entity;
					this.ImgList=[];
					if(json.Data.Info.Path!=''){
						this.OldList = json.Data.Info.Path.split(',');
					}
				});
			},
			PrevImg(urls,index){
				uni.previewImage({
					current:index,
					urls: urls,
					longPressActions: {
						itemList: ['保存图片'],
						success: data=> {
							uni.saveImageToPhotosAlbum({
								filePath: urls[data.index],
								success: d=> {
									this.Util.Toast("已保存至系统相册");
								}
							});
						}
					}
				});
			},
			formSubmit(e){
				var data = e.detail.value;
				data.Act="Upload_Img";
				data.ID=this.ID;
				data.Type=1;
				data.CaseID=this.CaseID;
				data.Method=this.Method;		
				this.Util.Loading("请稍候...");
				if(this.ImgList.length>0){
					var ImgList=this.Util.TranList(this.ImgList).reverse();
					this.Util.Upload(ImgList,"upload_file_o",[],list=>{
						data.Imgs = this.OldList.concat(list).join(",");
						this.Save(data);
					});
				}else{
					data.Imgs = this.OldList.join(",");
					this.Save(data);
				}
			},
			Save(data){						
				this.Util.PostAdmin(data, json=>{
					this.Util.HideLoading();
					if(json.State){
						this.Util.Toast(json.Msg, obj=>{
							uni.$emit("UploadSuccess","");
							this.Find_CaseInfo();
						});
					}else{
						this.Util.Alert(json.Msg);
					}
				});
			},
			ChooseImg() {
				uni.chooseImage({
					count: 9, // 默认9
					sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: res=> {
						var ImgList=[];
						res.tempFilePaths.forEach(item=>{
							ImgList.push(item);
						});						
						this.ImgList = this.ImgList.concat(ImgList);
					}
				});
			},
			DeleteImg1(index){
				const self = this
				this.Util.Confirm("您确定要删除吗？",item=>{
					self.OldList.splice(index,1)
				});
			},
			DeleteImg2(index){
				const self = this
				this.Util.Confirm("您确定要删除吗？",item=>{
					self.ImgList.splice(index,1)
				});
			}
		}
	}
</script>

<style lang="scss">
	.container2{padding-bottom: 180rpx;}	
</style>
